% mainroutine.m
% main routine in program cardiac assist
% calculate LV volume and pressure and aortic pressure and flow

whitebg('k');

clear all;
initialize_parameters;

HandleFig1=gcf;

HandleAxesPao=findobj(gcf,'Tag','AxesPao');
HandleAxesQao=findobj(gcf,'Tag','AxesQao');
HandleAxesPLV=findobj(gcf,'Tag','AxesPLV');
HandleAxesElastance=findobj(gcf,'Tag','AxesElastance');
HandleAxesZin=findobj(gcf,'Tag','AxesZin');
HandleAxesPQ=findobj(gcf,'Tag','AxesPQ');
HandleAxesPV=findobj(gcf,'Tag','AxesPV');
HandleAxesDelayAV=findobj(gcf,'Tag','AxesDelayAV');

% compute elastance curve and plot in figure

if ~exist('E_t_LV')
   calcE_t_LV;			% LV E_t is calculated in this routine
end   
draw_elastance;		% show elastance curves on screen
calcdEdt;				% dEdt is calculated in this routine

% compute input impedance model and plot modulus and phase angle in figure

inputimpedance;

% in this section, the set of differential equations is solved until convergence is reached
% the results are Pao, Qao, VLV, PLV and eventually Qpump as a function of time

solver_nopump_noLA_AOVR_MVR;

% results are known at this point; plot final results in figure;

axes(HandleAxesPQ);
set(HandleAxesPQ,'NextPlot','add');
plot(time,PLV,'r',time,Pao,'y');
ylabel('pressure (mmHg)');
xlabel('time (s)');
set(HandleAxesPQ,'XLim',[0 max(time)]);
set(HandleAxesPQ,'YLim',[0 1.1*max(PLV)]);
set(HandleAxesPQ,'Tag','AxesPQ');

axes(HandleAxesPV);
if loadelastA~=4
   plot(VLV,PLV,'m.',VLA,PLA,'y');
	set(HandleAxesPV,'NextPlot','add');
	xlabel('Volume');
	ylabel('Pressure');
	set(HandleAxesPV,'Xlim',[round(0.8*min([min(VLV) min(VLA)])) round(1.2*max([max(VLV) max(VLA)]))]);
	set(HandleAxesPV,'YLim',[0 1.1*max([max(PLV) max(PLA)])]);
	set(HandleAxesPV,'Tag','AxesPV');
else
   plot(VLV,PLV,'m.');
	set(HandleAxesPV,'NextPlot','add');
	xlabel('Volume');
	ylabel('Pressure');
	set(HandleAxesPV,'Xlim',[round(0.8*min(VLV)) round(1.2*max(VLV))]);
	set(HandleAxesPV,'YLim',[0 1.1*max(PLV)]);
	set(HandleAxesPV,'Tag','AxesPV');
end   

calculate_haemodynamics;

HandlePushbuttonSave=findobj(gcf,'Tag','PushbuttonSave');
set(HandlePushbuttonSave,'Enable','On');

